package com.rdgzs.framework.utilsTools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringUtils {
    public StringUtils() {
    }

    public static String charSetConvert(String src, String fromCharSet, String toCharSet) {
        if (src == null)
            return src;
        try {
            return new String(src.getBytes(fromCharSet), toCharSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String utf8ToISO(String src) {
        return charSetConvert(src, "UTF-8", "iso-8859-1");
    }

    public static String gbkToISO(String src) {
        return charSetConvert(src, "GBK", "iso-8859-1");
    }

    public static String isoToUTF8(String src) {
        return charSetConvert(src, "iso-8859-1", "UTF-8");
    }

    public static String isoToGBK(String src) {
        return charSetConvert(src, "iso-8859-1", "GBK");
    }

    public static String replace(String str, String pattern, String replace) {
        int s = 0;
        int e = 0;
        StringBuffer result = new StringBuffer();
        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e + pattern.length();
        }
        result.append(str.substring(s));
        return result.toString();
    }

    public static String repeat(String str, int repeat) {
        StringBuffer buffer = new StringBuffer(repeat * str.length());
        for (int i = 0; i < repeat; i++)
            buffer.append(str);
        return buffer.toString();
    }

    public static String left(String str, int len) {
        if (len < 0)
            throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
        if (str == null || str.length() <= len)
            return str;
        else
            return str.substring(0, len);
    }

    public static String right(String str, int len) {
        if (len < 0)
            throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
        if (str == null || str.length() <= len)
            return str;
        else
            return str.substring(str.length() - len);
    }

    public static String formatString(String src, char s2, int length) {
        String retValue = src;
        if (src == null || length <= 0)
            return null;
        if (src.length() > length)
            retValue = src.substring(0, length);
        for (int i = 0; i < length - src.length(); i++)
            retValue = s2 + retValue;
        return retValue;
    }

    public static String prefixStr(String source, char profix, int length) {
        String strRet = source;
        if (source == null)
            return strRet;
        if (source.length() >= length)
            strRet = source.substring(0, length);
        if (source.length() < length) {
            for (int i = 0; i < length - source.length(); i++)
                strRet = "" + profix + strRet;
        }
        return strRet;
    }

    public static String stringTrim(String value) {
        if (value == null)
            return "";
        else
            return value.trim();
    }

    public static String trimNull(String str) {
        String result = "";
        if (!"null".equals(str))
            result = stringTrim(str);
        return result;
    }

    public static String suffixStr(String source, char suffix, int length) {
        String strRet = source;
        if (source == null)
            return strRet;
        if (source.length() >= length)
            strRet = source.substring(0, length);
        if (source.length() < length) {
            for (int i = 0; i < length - source.length(); i++)
                strRet = strRet + suffix;
        }
        return strRet;
    }

    public static String trim4Number(String str) {
        str = stringTrim(str);
        if ("".equals(str))
            return "0";
        else
            return str;
    }

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str.trim());
    }

    public static boolean isNumber(String str) {
        boolean istrue = false;
        try {
            Integer.parseInt(str);
            istrue = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!istrue)
            try {
                Long.parseLong(str);
                istrue = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        if (!istrue)
            try {
                Float.parseFloat(str);
                istrue = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        if (!istrue)
            try {
                Double.parseDouble(str);
                istrue = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        return istrue;
    }

    public static ArrayList convertStrToArrayList(String str, String sp) {
        ArrayList al = new ArrayList();
        if (str == null)
            return al;
        String strArr[] = str.split(sp);
        for (int i = 0; i < strArr.length; i++)
            al.add(strArr[i]);
        return al;
    }

    public static byte[] convertInputStreamToByteArray(InputStream is) throws IOException {
        byte value[] = null;
        ByteArrayOutputStream baos = null;
        byte buffer[] = new byte[2048];
        int flag = 0;
        try {
            baos = new ByteArrayOutputStream();
            while ((flag = is.read(buffer, 0, 2048)) != -1) {
                baos.write(buffer, 0, flag);
                baos.flush();
            }
            value = baos.toByteArray();
        } catch (IOException ex) {
            logger.error("\u8FDB\u884C\u6D41\u538B\u7F29\u65F6\u53D1\u751F\u9519\u8BEF", ex);
            throw ex;
        } finally {
            if (baos != null)
                try {
                    baos.close();
                } catch (IOException ex) {
                    logger.error("\u5173\u95EDByteArrayOutputStream\u6D41\u65F6\u53D1\u751F\u9519\u8BEF", ex);
                    throw ex;
                }
        }
        return value;
    }

    public static InputStream convertByteArrayToInputStream(byte data[]) {
        if (data == null) {
            return null;
        } else {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            return bais;
        }
    }

    //返回随机数
    public static String getRandom() throws Exception {
        Random r = new Random();
        int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 100000; // 获取随机数
        return Integer.toString(rannum);
    }

    private static Logger logger;
    static {
        logger = LoggerFactory.getLogger(StringUtils.class);
    }
}
